package com.czz.portal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.portal.entity.Content;
import com.czz.portal.entity.Question;
import com.czz.portal.entity.Tag;
import com.czz.portal.query.QuestionQuery;
import com.czz.portal.service.QuestionService;
import com.czz.portal.service.TagService;
import com.czz.portal.vo.QuestionDetailVo;
import com.czz.portal.vo.RecommendQuestionVo;
import com.czz.portal.vo.UserQuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: QuestionController
 * @Author Czz
 * @Date 2020/2/13
 * @Time 13:30
 **/
@RestController
@RequestMapping(value = "/question")
public class QuestionController {

    @Autowired
    QuestionService questionService;

    @Autowired
    TagService tagService;

    /**
    * @Description 分页查询问答中心问答列表
    * @Param [page, limit, questionQuery]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/13
    * @Time 13:51
    **/
    @GetMapping(value = "/{page}/{limit}")
    public R getQuestionPageList(@PathVariable Long page, @PathVariable Long limit, QuestionQuery questionQuery){
        if (page <= 0 || limit <= 0){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Page<Question> pageParam = new Page<>(page, limit);
        pageParam.setRecords(questionService.getQuestionPageList(pageParam,questionQuery));
        Long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", pageParam.getRecords());
    }

    /**
    * @Description 获取所有问答标签
    * @Param []
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/13
    * @Time 16:55
    **/
    @GetMapping(value = "/tag")
    public R getQuestionTagList(){
        List<Tag> tagList = tagService.getQuestionTagList();
        return R.ok().data("item",tagList);
    }

    /**
    * @Description 根据问答id获取问答详情,并且点击量加一
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/14
    * @Time 10:49
    **/
    @GetMapping(value = "/detail/{id}")
    public R getQuestionDetailById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        questionService.updateQuestionClickById(id);
        QuestionDetailVo questionDetailVo = questionService.getQuestionDetailById(id);
        return R.ok().data("item",questionDetailVo);
    }

    /**
    * @Description 获取推荐相关问题
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/14
    * @Time 13:33
    **/
    @GetMapping(value = "/recommend/{id}")
    public R getRecommendQuestion(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        List<RecommendQuestionVo> recommendQuestionVoList = questionService.getRecommendQuestion(id);
        return R.ok().data("items",recommendQuestionVoList);
    }

    /**
    * @Description 发布问题
    * @Param [question, content]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/15
    * @Time 12:06
    **/
    @PostMapping(value = "")
    public R saveQuestion(Question question, Content content){
        if (ObjectUtils.isEmpty(question) || ObjectUtils.isEmpty(content)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        questionService.saveQuestion(question,content);
        return R.ok();
    }

    /**
    * @Description 获取用户提问分页列表
    * @Param [page, limit, userId]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/15
    * @Time 17:46
    **/
    @GetMapping(value = "/user/{page}/{limit}/{userId}")
    public R getUserQuestionPageList(@PathVariable Long page,@PathVariable Long limit,@PathVariable String userId){
        if (page <= 0 || limit <= 0 || StringUtils.isEmpty(userId)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Page<UserQuestionVo> pageParam = new Page<>(page, limit);
        pageParam.setRecords(questionService.getUserQuestionPageList(pageParam,userId));
        Long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", pageParam.getRecords());
    }

    /**
     * @Description 根据问答id获取问答状态
     * @Param [id]
     * @Return com.czz.common.vo.R
     * @Author Czz
     * @Date 2020/3/25
     * @Time 18:11
     **/
    @GetMapping(value = "{id}")
    public R getQuestionStatus(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Question question = questionService.getById(id);
        return R.ok().data("item",question.getStatus());
    }
}
